<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	public function __construct(){
		parent::__construct();
		// Your own constructor code
		$this->load->helper('url');	 // used for anchors
		$this->load->helper('form');
		
		// load required models
		$this->load->model('registration_model');
	}
	
	public function index(){
		$data['main_content'] = 'welcome_message';
		$data['woo'] = $this->session->all_userdata();
		$this->load->view('includes/template',$data);
	}
	
	public function login(){
		$data['main_content'] = 'login_view';
		$this->load->view('includes/template',$data);
	}
	
	public function newuser(){
		$data['main_content'] = 'newuser_view';
		$data['message'] = '';
		$this->load->view('includes/template',$data);
	}
	
	public function register(){
		$data['main_content'] = 'registration_view';
		$this->load->view('includes/template',$data);
	}
	
	public function registration_request(){
		  // recaptcha code start 	
		  require_once('recaptchalib.php');
		  $privatekey = "6LcHGsgSAAAAALMCZ6UHLWoxxOWa375nEjuFbJPA"; //secret!
		  $resp = recaptcha_check_answer ($privatekey,
		                                $_SERVER["REMOTE_ADDR"],
		                                $_POST["recaptcha_challenge_field"],
		                                $_POST["recaptcha_response_field"]);
		
		  if (!$resp->is_valid) {
		    // What happens when the CAPTCHA was entered incorrectly
		    $data['message'] = "The reCAPTCHA wasn't entered correctly. Go back and try it again. <br/>";
		    //reCAPTCHA said: " . $resp->error ;
		    
		    $data['main_content'] = 'newuser_view';
			$this->load->view('includes/template',$data);
		  
		  } else {
		  	//recaptcha code end
		    // Your code here to handle a successful verification
			
			/* store the registration request in the database */
			$status = $this->registration_model->save_registration_request($_POST);
			
			/* send email to the super administrator 
			$this->load->library('email');
	
			$this->email->from('kedar@kedart.com', 'Virtual Heart Museum');
			$this->email->to('thakur.kedar@gmail.com');
			//$this->email->cc('another@another-example.com');
			//$this->email->bcc('them@their-example.com');
			
			$this->email->subject('User Registration Request');
			
			$email_content = "The virtual heart museum has received a new user registration request.<br/>";
			$email_content .= "<br/>User Name: ".$_POST['name']; 
			$email_content .= "<br/>Institution: ".$_POST['institution'];
			$email_content .= "<br/>Department: ".$_POST['department'];
			$email_content .= "<br/>Address: ".$_POST['address'];
			$email_content .= "<br/>Phone: ".$_POST['phone'];
			$email_content .= "<br/>Email: ".$_POST['email'];
			$email_content .= "<br/>Additional Information: ".$_POST['info'];
			
			$this->email->message($email_content);
			
			$this->email->send();
			
			echo $this->email->print_debugger();*/
			
			if($status == 1){
				$data['message'] = "<br/><br/> Your request has been submitted. We will get back to you soon.";
			}
			else{
				$data['message'] = "<br/><br/> There was an error saving your request. Please email Prof. Abc (xyz at somedomain dot edu).";
				$data['message'] .= "<br/><br/> We apologize for the inconvenience.<br/>"; 
			}
			$data['main_content'] = 'registration_complete_view';
			$this->load->view('includes/template',$data);
		  }

	}
	
	function view_registration_requests(){
		$data['search_results'] = $this->registration_model->get_registration_requests();
		$this->load->view('registration_requests_view',$data);
	}
	
	function add_new_specimen(){
		$data['main_content'] = 'add_new_specimen_view';
		$this->load->view('includes/template',$data);
	}
	
	function submit_specimen(){
		
	}
	
	function process_login(){
		echo "test 123" ;
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */